package com.bbx.taxi.client.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.util.LogUtils;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Bean.Extra.ActivityDetialMsg;
import com.bbx.taxi.client.widget.pop.SharePopView;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.sso.UMSsoHandler;

import butterknife.Bind;

/**
 * @Path com.bbx.taxi.client.Activity.ActivityDetialActivity.java
 * @Author 严旭文
 * @Date 2016-5-24 下午2:21:52
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class ActivityDetialActivity extends BaseBBXActivity {
	@Bind(R.id.webview)
	WebView webview;
	/**分享POP*/
	public SharePopView mSharePopView;

	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_recommen);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initView() {
		mSharePopView=new SharePopView(context,null);
		String url = getIntent().getExtras().getString(ActivityDetialMsg.URL);
		String name = getIntent().getExtras().getString(ActivityDetialMsg.NAME);
		setTitle(name);
		if (url == null || url.equals("")) {
			ToastUtil.showToast(context, R.string.no_network);
			finish();
		}
		if (loaddialog != null && !loaddialog.isShowing()) {
			loaddialog.show();
		}
		webview.getSettings().setAllowFileAccess(true);
	        //如果访问的页面中有Javascript，则webview必须设置支持Javascript
		webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webview.getSettings().setAllowFileAccess(true);
		webview.getSettings().setAppCacheEnabled(true);
		webview.getSettings().setDomStorageEnabled(true);
		webview.getSettings().setDatabaseEnabled(true);
		webview.getSettings().setGeolocationEnabled(true);  
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setSupportZoom(true);
//		int fontSize = 1;
//		if (webview.getSettings().getTextSize() == WebSettings.TextSize.SMALLEST) {
//		   fontSize = 1;
//		  } else if (webview.getSettings().getTextSize() == WebSettings.TextSize.SMALLER) {
//		   fontSize = 2;
//		  } else if (webview.getSettings().getTextSize() == WebSettings.TextSize.NORMAL) {
//		   fontSize = 3;
//		  } else if (webview.getSettings().getTextSize() == WebSettings.TextSize.LARGER) {
//		   fontSize = 3;
//		  } else if (webview.getSettings().getTextSize() == WebSettings.TextSize.LARGEST) {
//		   fontSize = 3;
//		  }
//		  switch (fontSize) {
//
//          case 1:
//        	  webview.getSettings().setTextSize(WebSettings.TextSize.SMALLEST);
//              break;
//          case 2:
//        	  webview.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
//              break;
//          case 3:
//        	  webview.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
//              break;
//          case 4:
//        	  webview.getSettings().setTextSize(WebSettings.TextSize.LARGER);
//              break;
//          case 5:
//        	  webview.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
//              break;
//          }
		webview.loadUrl(url);
		//增加接口方法,让html页面调用  
        webview.addJavascriptInterface(this,"ncp");  
		webview.setWebViewClient(new WebViewClient(){
       	  @Override
             public void onPageFinished(WebView view, String url) 
             {
       		  if (loaddialog != null ) {
       			loaddialog.dismiss();
       			}
                 super.onPageFinished(view, url);
             }
       	  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
       		if( url.startsWith("http:") || url.startsWith("https:") ) {  
                return false;  
            }  
  
            // Otherwise allow the OS to handle things like tel, mailto, etc.  
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
            startActivity( intent );  
            return true;  
          }  
       });
		 webview.setWebChromeClient(new WebChromeClient(){
		        @Override
		        public void onGeolocationPermissionsShowPrompt(String origin,
		        		android.webkit.GeolocationPermissions.Callback callback) {
		            // TODO Auto-generated method stub
		            super.onGeolocationPermissionsShowPrompt(origin, callback);
		            callback.invoke(origin, true, false);
		        }
		    });

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler =mSharePopView.getController().getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}
	@JavascriptInterface
	public void onShare(final String type,final String url,final String title,final String text){
		new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... arg0) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
			   if(type.equals("weixin")){
				   mSharePopView.performShare(SHARE_MEDIA.WEIXIN, url,title,text,0);
			   }else if(type.equals("friend")){
				   mSharePopView. performShare(SHARE_MEDIA.WEIXIN_CIRCLE,  url,title,text,0);
			   }
			   else if(type.equals("xinlang")){
				   mSharePopView. performShare(SHARE_MEDIA.SINA,url,title,text,0);
			   }
				super.onPostExecute(result);
			}
		  	
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
}
